#include "LinkedList.h"
#include "Node_QuickSort.h"
#include <fstream> 
#include <iostream> 

#define SLOT 255
#define AMAX 2000

int hashstring(char* str);
LinkedList* HashCount(FILE *fp);
void print_20_result(LinkedList* _A);

int hashstring(char* str){
  int hash=0;
  int i=0xff;
  int c;
  while (c = *str++){
    if ('A' <= c && c <= 'Z')
      c = c + 32;
    hash = hash + c;
  }
  hash = hash & i;
  return hash;  
}

LinkedList* HashCount(FILE *fp){
  LinkedList* AL = new LinkedList[SLOT];
  char str[MAX];
  char ch;
  int t=0,h;
  while (!feof(fp)){
    ch = fgetc(fp);
    if (feof(fp))
      break;
    if (( ch >= 'a' && ch <= 'z' )||(ch >= 'A' && ch <= 'Z')||ch =='-'||ch == 39){
      if (ch >= 'A' && ch <= 'Z')
	ch = ch + 32;
      str[t] = ch;
      t++;
    }
    else{
      if (t > 0){
	str[t] = '\0';
	h = hashstring(str);
	if (AL[h].find(str) == NULL)
	  AL[h].append(str);
	else{
	  Node* p = AL[h].find(str);
	  p->set_num();
	}
	t = 0;
	strcpy(str,"");
      }
    }
  }
  return AL;
}




void print_20_result(LinkedList* _A){
  Node B[AMAX];
  int t=0;
  for (int i = 0; i < SLOT; i++){
    if (_A[i].isempty() == 1){
      Node* p = _A[i].head;
      B[t].set_num(p->get_num());
      B[t].set_str(p->get_str());
      t++;
      while (p->get_next() != NULL){
	p = p->get_next();
	B[t].set_num(p->get_num());
	B[t].set_str(p->get_str());
	t++;
      }
    }
  }
  Node_quicksort(B,t);
  for (int i = 0; i < 20; i++)
    B[i].print();
}


